﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
using System.Text;

namespace V.WP.PM_Communicate
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            try
            {
                InitializeComponent();

                Common.CopyContentToIsolatedStorage("zip/html.zip");
                Common.CopyContentToIsolatedStorage("dict.txt");
                MyZip.UnZip("zip/html.zip");
            }
            catch { }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                BindData();
            }
            catch { }
        }

        private void BindData()
        {
            int i = 0;
            List<Dict> listDict = new List<Dict>();
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists("dict.txt"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("dict.txt", FileMode.Open, FileAccess.Read))
                    {
                        if (stream.Length > 0)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                string line = String.Empty;
                                while ((line = reader.ReadLine()) != null && line.Length > 0)
                                {
                                    Dict d = new Dict();
                                    d.No = ++i;
                                    d.Title = line;
                                    listDict.Add(d);
                                }
                            }
                        }
                    }
                }
            }
            pList.ItemsSource = listDict;
        }

        private void pList_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                NavigationService.Navigate(new Uri(String.Format("/Detail.xaml?no={0}", pList.SelectedIndex + 1), UriKind.Relative));
            }
            catch { }
        }
    }
}